package com.ruoyi.web.controller.tool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("cp")
public class HttpController {
    @RequestMapping("testForm")
    public String testForm(@RequestParam String key, @RequestParam String value) {
        log.error("key:{},value:{}", key, value);
        return "ok";
    }

    @RequestMapping("httpJSON")
    public JSONObject httpJSON(@RequestBody(required = false) JSONObject requestBody) {
        return JSONObject.parseObject(httpStr(requestBody));
    }

    @RequestMapping("httpArray")
    public JSONArray httpArray(@RequestBody(required = false) JSONObject requestBody) {
        return JSONArray.parseArray(httpStr(requestBody));
    }

    @RequestMapping("httpStr")
    public String httpStr(@RequestBody(required = false) JSONObject requestBody) {
        String url = requestBody.getString("url");
        String method = requestBody.getString("method");
        String contentType = requestBody.getString("contentType");
        JSONObject header = requestBody.getJSONObject("header");

        HttpRequest request = null;
        if ("get".equals(method)) {
            request = HttpUtil.createGet(url);
        } else {
            request = HttpUtil.createPost(url);
        }

        if (header != null) {
            Iterator<String> iterator = header.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                request.header(key, header.getString(key));
            }
        }

        JSONObject body = requestBody.getJSONObject("body");

        if (contentType != null) {
            switch (contentType) {
                case "x-www-form-urlencoded":
                    request.contentType(ContentType.FORM_URLENCODED.toString());
                    request.form(body);
                    break;
                case "json":
                    request.contentType(ContentType.JSON.toString());
                    request.body(body.toJSONString());
                    break;
            }
        } else if (body != null) {
            request.body(body.toJSONString());
        }
        String res = request.execute().body();
        return res;
    }

    @RequestMapping("httpAo5")
    public JSONObject httpAo5(@RequestBody(required = false) JSONObject requestBody) {
//        String str = FileUtil.readString("D:\\乱七八糟\\ao5.txt", "utf8");
        JSONObject jsonObject = httpJSON(requestBody);
        return getData(jsonObject);
    }

    public static JSONObject getData(JSONObject jsonObject) {
//        String str = FileUtil.readString("D:\\乱七八糟\\ao5.txt", "utf8");
//        jsonObject = JSONObject.parseObject(str);

        JSONObject data = new JSONObject();

        if (CollUtil.isEmpty(jsonObject)) {
            return data;
        }
        JSONObject result = jsonObject.getJSONObject("result");

        JSONArray datas = result.getJSONArray("data");
        List<String> names = Arrays.asList("preDrawTime", "preDrawIssue", "preDrawCode");
        for (int i = 0; i < datas.size(); i++) {
            JSONObject item = datas.getJSONObject(i);
            // 创建一个新的JSONObject只包含需要的字段
            JSONObject newItem = new JSONObject();
            for (String name : names) {
                if (item.containsKey(name)) {
                    newItem.put(name, item.get(name));
                }
            }
            // 替换原item
            datas.set(i, newItem);
        }
        data.put("result", result);
        data.put("errorCode", 0);
        return data;
    }
}
